/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

using System;
using System.Collections.Generic;

namespace FaceCat {
    /// <summary>
    /// 表格行的样式
    /// </summary>
    public class FCGridRowStyle {
        public long m_backColor = FCColor.None;

        /// <summary>
        /// 获取背景色
        /// </summary>
        public virtual long getBackColor() {
            return m_backColor;
        }

        /// <summary>
        /// 设置背景色
        /// </summary>
        public virtual void setBackColor(long value) {
            m_backColor = value;
        }

        public FCFont m_font = new FCFont("Default", 14, false, false, false);

        /// <summary>
        /// 获取字体
        /// </summary>
        public virtual FCFont getFont() {
            return m_font;
        }

        /// <summary>
        /// 设置字体
        /// </summary>
        public virtual void setFont(FCFont value) {
            m_font = value;
        }

        public long m_hoveredBackColor = FCColor.Hovered;

        /// <summary>
        /// 获取触摸悬停行的背景色
        /// </summary>
        public virtual long getHoveredBackColor() {
            return m_hoveredBackColor;
        }

        /// <summary>
        /// 设置触摸悬停行的背景色
        /// </summary>
        public virtual void setHoveredBackColor(long value) {
            m_hoveredBackColor = value;
        }

        public long m_hoveredTextColor = FCColor.Text;

        /// <summary>
        /// 获取触摸悬停行的前景色
        /// </summary>
        public virtual long getHoveredTextColor() {
            return m_hoveredTextColor;
        }

        /// <summary>
        /// 设置触摸悬停行的前景色
        /// </summary>
        public virtual void setHoveredTextColor(long value) {
            m_hoveredTextColor = value;
        }

        public long m_selectedBackColor = FCColor.Pushed;

        /// <summary>
        /// 获取选中行的背景色
        /// </summary>
        public virtual long getSelectedBackColor() {
            return m_selectedBackColor;
        }

        /// <summary>
        /// 设置选中行的背景色
        /// </summary>
        public virtual void setSelectedBackColor(long value) {
            m_selectedBackColor = value;
        }

        public long m_selectedTextColor = FCColor.Text;

        /// <summary>
        /// 获取选中行的前景色
        /// </summary>
        public virtual long getSelectedTextColor() {
            return m_selectedTextColor;
        }

        /// <summary>
        /// 设置选中行的文字色
        /// </summary>
        public virtual void setSelectedTextColor(long value) {
            m_selectedTextColor = value;
        }

        public long m_textColor = FCColor.Text;

        /// <summary>
        /// 获取文字颜色
        /// </summary>
        public virtual long getTextColor() {
            return m_textColor;
        }

        /// <summary>
        /// 设置文字颜色
        /// </summary>
        public virtual void setTextColor(long value) {
            m_textColor = value;
        }
    }

    /// <summary>
    /// 单元格
    /// </summary>
    public class FCGridRow : FCAttribute {
        /// <summary>
        /// 创建行
        /// </summary>
        public FCGridRow() {
        }

        /// <summary>
        /// 析构函数
        /// </summary>
        ~FCGridRow() {
            delete();
        }

        /// <summary>
        /// 所有单元格
        /// </summary>
        public ArrayList<FCGridCell> m_cells = new ArrayList<FCGridCell>();

        /// <summary>
        /// 编辑状态
        /// </summary>
        public int m_editState;

        public bool m_allowEdit;

        /// <summary>
        /// 获取是否允许编辑
        /// </summary>
        public virtual bool allowEdit() {
            return m_allowEdit;
        }

        /// <summary>
        /// 设置是否允许编辑
        /// </summary>
        public virtual void setAllowEdit(bool allowEdit) {
            m_allowEdit = allowEdit;
        }

        public FCRect m_bounds;

        /// <summary>
        /// 获取显示区域
        /// </summary>
        public virtual FCRect getBounds() {
            return m_bounds;
        }

        /// <summary>
        /// 设置显示区域
        /// </summary>
        public virtual void setBounds(FCRect value) {
            m_bounds = value;
        }

        public FCView m_editButton;

        /// <summary>
        /// 获取编辑按钮
        /// </summary>
        public virtual FCView getEditButton() {
            return m_editButton;
        }

        /// <summary>
        /// 设置编辑按钮
        /// </summary>
        public virtual void setEditButton(FCView editButton) {
            m_editButton = editButton;
        }

        public FCGrid m_grid;

        /// <summary>
        /// 获取所在表格
        /// </summary>
        public virtual FCGrid getGrid() {
            return m_grid;
        }

        /// <summary>
        /// 设置所在表格
        /// </summary>
        public virtual void setGrid(FCGrid value) {
            m_grid = value;
        }

        public int m_height = 20;

        /// <summary>
        /// 获取行高
        /// </summary>
        public virtual int getHeight()
        {
            return m_height;
        }

        /// <summary>
        /// 设置行高
        /// </summary>
        public virtual void setHeight(int value)
        {
            m_height = value;
        }

        public int m_horizontalOffset = 0;

        /// <summary>
        /// 获取横向滚动距离
        /// </summary>
        public virtual int getHorizontalOffset() {
            return m_horizontalOffset;
        }

        /// <summary>
        /// 设置横向滚动距离
        /// </summary>
        public virtual void setHorizontalOffset(int value) {
            m_horizontalOffset = value;
        }

        public int m_index = -1;

        /// <summary>
        /// 获取索引
        /// </summary>
        public virtual int getIndex() {
            return m_index;
        }

        /// <summary>
        /// 设置索引
        /// </summary>
        public virtual void setIndex(int value) {
            m_index = value;
        }

        public bool m_isDeleted;

        /// <summary>
        /// 获取或设置是否已被销毁
        /// </summary>
        public virtual bool isDeleted() {
            return m_isDeleted; 
        }

        public Object m_tag = null;

        /// <summary>
        /// 获取TAG值
        /// </summary>
        public virtual Object getTag() {
            return m_tag;
        }

        /// <summary>
        /// 设置TAG值
        /// </summary>
        public virtual void setTag(Object value) {
            m_tag = value;
        }

        public bool m_visible = true;

        /// <summary>
        /// 获取是否可见
        /// </summary>
        public virtual bool isVisible()
        {
            return m_visible;
        }

        /// <summary>
        /// 设置是否可见
        /// </summary>
        public virtual void setVisible(bool value)
        {
            m_visible = value;
        }

        public int m_visibleIndex = -1;

        /// <summary>
        /// 获取可见索引
        /// </summary>
        public virtual int getVisibleIndex() {
            return m_visibleIndex;
        }

        /// <summary>
        /// 设置可见索引
        /// </summary>
        public virtual void setVisibleIndex(int value) {
            m_visibleIndex = value;
        }

        /// <summary>
        /// 添加单元格
        /// </summary>
        /// <param name="column">列索引</param>
        /// <param name="cell">单元格</param>
        public virtual void addCell(FCGridColumn column, FCGridCell cell) {
            cell.setGrid(m_grid);
            cell.setColumn(column);
            cell.setRow(this);
            m_cells.add(cell);
            cell.onAdd();
        }

        /// <summary>
        /// 添加单元格
        /// </summary>
        /// <param name="columnIndex">列索引</param>
        /// <param name="cell">单元格</param>
        public virtual void addCell(int columnIndex, FCGridCell cell) {
            cell.setGrid(m_grid);
            cell.setColumn(m_grid.getColumn(columnIndex));
            cell.setRow(this);
            m_cells.add(cell);
            cell.onAdd();
        }

        /// <summary>
        /// 添加单元格
        /// </summary>
        /// <param name="columnName">列名</param>
        /// <param name="cell">单元格</param>
        public virtual void addCell(String columnName, FCGridCell cell) {
            cell.setGrid(m_grid);
            cell.setColumn(m_grid.getColumn(columnName));
            cell.setRow(this);
            m_cells.add(cell);
            cell.onAdd();
        }

        /// <summary>
        /// 调用方法
        /// </summary>
        /// <param name="funcName">名称</param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        public virtual String callFunction(String funcName, String parameters)
        {
            return "";
        }

        /// <summary>
        /// 清除单元格
        /// </summary>
        public virtual void clearCells() {
            int cellSize = m_cells.size();
            for (int i = 0; i < cellSize; i++) {
                m_cells.get(i).onRemove();
                m_cells.get(i).delete();
            }
            m_cells.clear();
        }

        /// <summary>
        /// 销毁对象
        /// </summary>
        public virtual void delete() {
            if (!m_isDeleted) {
                m_editButton = null;
                m_isDeleted = !m_isDeleted;
                int cellSize = m_cells.size();
                for (int i = 0; i < cellSize; i++) {
                    m_cells.get(i).delete();
                }
                m_cells.clear();
            }
        }

        /// <summary>
        /// 根据列获取单元格
        /// </summary>
        /// <param name="column">列</param>
        /// <returns>单元格</returns>
        public virtual FCGridCell getCell(FCGridColumn column) {
            int cellSize = m_cells.size();
            for (int i = 0; i < cellSize; i++) {
                if (m_cells.get(i).getColumn() == column) {
                    return m_cells.get(i);
                }
            }
            return null;
        }

        /// <summary>
        /// 根据列的索引获取单元格
        /// </summary>
        /// <returns>单元格</returns>
        public virtual FCGridCell getCell(int columnIndex) {
            int cellsSize = m_cells.size();
            if (cellsSize > 0) {
                if (columnIndex >= 0 && columnIndex < cellsSize) {
                    if (m_cells.get(columnIndex).getColumn().getIndex() == columnIndex) {
                        return m_cells.get(columnIndex);
                    }
                }
                for (int i = 0; i < cellsSize; i++) {
                    if (m_cells.get(i).getColumn().getIndex() == columnIndex) {
                        return m_cells.get(i);
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// 根据列的名称获取单元格
        /// </summary>
        /// <param name="columnName">列名</param>
        /// <returns>单元格</returns>
        public virtual FCGridCell getCell(String columnName) {
            int cellSize = m_cells.size();
            for (int i = 0; i < cellSize; i++) {
                if (m_cells.get(i).getColumn().getName() == columnName)
                {
                    return m_cells.get(i);
                }
            }
            return null;
        }

        /// <summary>
        /// 获取所有单元格
        /// </summary>
        /// <returns>所有单元格</returns>
        public virtual ArrayList<FCGridCell> getCells() {
            return m_cells;
        }

        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <param name="value">返回属性值</param>
        /// <param name="type">返回属性类型</param>
        public virtual void getAttribute(String name, ref String value, ref String type) {
            if (name == "allowedit") {
                type = "bool";
                value = FCTran.boolToStr(allowEdit());
            }
            else if (name == "height") {
                type = "int";
                value = FCTran.intToStr(getHeight());
            }
            else if (name == "visible") {
                type = "int";
                value = FCTran.boolToStr(isVisible());
            }
            else {
                type = "undefined";
                value = "";
            }
        }

        /// <summary>
        /// 获取属性名称列表
        /// </summary>
        /// <returns></returns>
        public virtual ArrayList<String> getAttributeNames() {
            ArrayList<String> attributeNames = new ArrayList<String>();
            attributeNames.AddRange(new String[] { "AllowEdit", "EditorWidth", "Height", "Visible" });
            return attributeNames;
        }

        /// <summary>
        /// 添加行方法
        /// </summary>
        public virtual void onAdd() {
            int cellSize = m_cells.size();
            for (int i = 0; i < cellSize; i++) {
                m_cells.get(i).onAdd();
            }
        }

        /// <summary>
        /// 重绘方法
        /// </summary>
        /// <param name="paint">绘图对象</param>
        /// <param name="clipRect">裁剪矩形</param>
        /// <param name="isAlternate">是否交替行</param>
        public virtual void onPaint(FCPaint paint, FCRect clipRect, bool isAlternate) {
            long backColor = FCColor.None;
            FCGridRowStyle rowStyle = m_grid.getRowStyle();
            if (isAlternate) {
                FCGridRowStyle alternateRowStyle = m_grid.getAlternateRowStyle();
                if (alternateRowStyle != null) {
                    rowStyle = alternateRowStyle;
                }
            }
            if (rowStyle != null) {
                bool selected = false;
                if (m_grid.getSelectionMode() == FCGridSelectionMode.SelectFullRow) {
                    ArrayList<FCGridRow> selectedRows = m_grid.getSelectedRows();
                    int selectedRowsSize = selectedRows.size();
                    for (int i = 0; i < selectedRowsSize; i++) {
                        if (selectedRows.get(i) == this) {
                            selected = true;
                            break;
                        }
                    }
                }
                if (selected) {
                    backColor = rowStyle.getSelectedBackColor();
                } else if (this == m_grid.getHoveredRow()) {
                    backColor = rowStyle.getHoveredBackColor();
                } else {
                    backColor = rowStyle.getBackColor();
                }
                paint.fillRect(backColor, clipRect);
            }
        }
        

        /// <summary>
        /// 重绘边线方法
        /// </summary>
        /// <param name="paint">绘图对象</param>
        /// <param name="clipRect">裁剪矩形</param>
        /// <param name="isAlternate">是否交替行</param>
        public virtual void onPaintBorder(FCPaint paint, FCRect clipRect, bool isAlternate) {

        }

        /// <summary>
        /// 移除行方法
        /// </summary>
        public virtual void onRemove() {
            int cellSize = m_cells.size();
            for (int i = 0; i < cellSize; i++) {
                m_cells.get(i).onRemove();
            }
        }

        /// <summary>
        /// 缩放
        /// </summary>
        /// <param name="scaleFactorX">横向比例</param>
        /// <param name="scaleFactorY">纵向比例</param>
        public virtual void onScaling(double scaleFactorX, double scaleFactorY) {
            m_height = (int)(int)(m_height * scaleFactorY);
            int cellSize = m_cells.size();
            for (int i = 0; i < cellSize; i++) {
                m_cells.get(i).onScaling(scaleFactorX, scaleFactorY);
            }
        }

        /// <summary>
        /// 移除单元格
        /// </summary>
        /// <param name="column">列</param>
        public virtual void removeCell(FCGridColumn column) {
            int cellSize = m_cells.size();
            for (int i = 0; i < cellSize; i++) {
                FCGridCell cell = m_cells.get(i);
                if (cell.getColumn() == column) {
                    m_cells.remove(cell);
                    cell.onRemove();
                    break;
                }
            }
        }

        /// <summary>
        /// 移除单元格
        /// </summary>
        /// <param name="columnIndex">列索引</param>
        public virtual void removeCell(int columnIndex) {
            int cellSize = m_cells.size();
            if (columnIndex >= 0 && columnIndex < cellSize) {
                FCGridCell cell = m_cells.get(columnIndex);
                if (cell.getColumn().getIndex() == columnIndex) {
                    m_cells.remove(cell);
                    cell.onRemove();
                    return;
                }
                for (int i = 0; i < cellSize; i++) {
                    cell = m_cells.get(i);
                    if (cell.getColumn().getIndex() == columnIndex) {
                        m_cells.remove(cell);
                        cell.onRemove();
                        break;
                    }
                }
            }
        }

        /// <summary>
        /// 移除单元格
        /// </summary>
        /// <param name="columnName">列名</param>
        public virtual void removeCell(String columnName) {
            int cellSize = m_cells.size();
            for (int i = 0; i < cellSize; i++) {
                FCGridCell cell = m_cells.get(i);
                if (cell.getColumn().getName() == columnName) {
                    m_cells.remove(cell);
                    cell.onRemove();
                    break;
                }
            }
        }

        /// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <param name="value">属性值</param>
        public virtual void setAttribute(String name, String value) {
            if (name == "allowedit") {
                setAllowEdit(FCTran.strToBool(value));
            }
            else if (name == "height") {
                setHeight(FCTran.strToInt(value));
            }
            else if (name == "visible") {
                setVisible(FCTran.strToBool(value));
            }
        }
    }
}
